# -*- coding: utf-8 -*-
# author: AZJ
# date: 2024/11/20
# 假如命运扼住了你的咽喉，你就去挠她的咯吱窝
import pytest
import requests

'''
将session作为夹具内的返回值操作
'''
@pytest.fixture(scope='session')
def session(request):
    session = requests.Session()
    user_data = '{"loginname":"guest","password":"guest"}'
    headers = {'Content-Type': 'application/json;*/*'}
    token_re = session.post('http://192.168.235.147:8080/v7/login', headers=headers,data=user_data)
    print()
    tkn = token_re.json()["token"]
    Cookie = f"Bearer {tkn}"
    headers['Authorization'] =  Cookie
    session.headers.update(headers)
    yield session
    session.close()

def test_new_user(session):
    null = None
    data = {"updatedate": null, "pimpersonid": null, "pimpersonname": "b", "orgid": null, "orgsectorid": null,
            "yglx": null, "pimpersonid2": null, "jlczz": null, "ygbh": "a", "zjlx": "30", "zjhm": "c",
            "lxdh": "11111111111", "ygzt": "30", "csrq": null, "xb": null, "nj": null, "pimpersonabilities": [],
            "pimpapers": [], "pimdistirbutions": [], "pimtitles": [], "pimvocationals": [], "pimarmycadres": [],
            "pimcorrectionapplies": [], "pimvacations": [], "trmtrainpeople": [], "pimcontracts": [],
            "pimeducations": [], "pimpersonchanges": [], "pimachievements": [], "pimresearchfindings": [],
            "pimexitandentries": [], "pimrewardpunishments": [], "pimpatents": [], "pimlanguageabilities": [],
            "pimarchives": [], "pimfaminfos": [], "attendancemreportmxes": [], "pimworkhistories": []}
    r = session.post('http://192.168.235.147:8080/pimpeople', json=data)
    print(r.status_code)

def test_upload(session):
    file = []
    url = 'http://192.168.235.147:8080/ibizutil/upload'
    boundary = "----WebKitFormBoundaryrjBbSqGXI0twpGsx"
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
               'Content-Type': f'multipart/form-data; boundary={boundary}',
               'srfappdata': '[object Object]',
               'Accept': '*/*',
               'Accept-Encoding': 'gzip, deflate',
               'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
               'Origin': 'http://192.168.235.147:8080',
               'Referer': 'http://192.168.235.147:8080/'}
    session.headers.update(headers)
    for i in range(1,10):
        f = open(f'D:\\11\\pic\\00{i}.jpg', 'rb')
        f_name = f"00{i}.jpg"
        data = "\r\n--" + boundary + (f"\r\nContent-Disposition: form-data; "
                                      f"name=\"file\"; filename=\"{f_name}\"\r\n"
                                      f"Content-Type: image/jpeg\r\n\r\n") + f.read().decode("latin1")
        data += "\r\n--{}--\r\n".format(boundary)

        r = session.post(url, data=data.encode('latin1'))
        print(r.status_code)
        if r.status_code == 200:
            file.append(r.json())
    print(file)
    # print(data.encode('latin1'))
